package com.example.chess.config;

import com.example.chess.api.ChatAPI;
import com.example.chess.api.GameAPI;
import com.example.chess.api.MatchAPI;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import javax.annotation.Resource;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Resource
    private MatchAPI matchAPI;

    @Resource
    private GameAPI gameAPI;

    @Resource
    private ChatAPI chatAPI;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(matchAPI, "/match").
                // 将前面的 HttpSession 拿到 webSocket的 session里去
                addInterceptors(new HttpSessionHandshakeInterceptor());

        registry.addHandler(gameAPI, "/game").
                // 将前面的 HttpSession 拿到 webSocket的 session里去
                        addInterceptors(new HttpSessionHandshakeInterceptor());
        registry.addHandler(chatAPI, "/chat").
                // 将前面的 HttpSession 拿到 webSocket的 session里去
                        addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
